<?php
/**
 * APP会员
 *
 *
 * * @网店运维 (c) 2015-2018 ShopWWI Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权
 */



defined('ByShopWWI') or exit('Access Invalid!');

class memberControl extends MobileControl{

    public function __construct(){
       if(empty($_SESSION['is_login'])){
       	redirect('index.php?act=login');
       }
        $model_cart = Model('cart');
        if ($_SESSION['member_id']){
            //登录后
            $cart_list  = $model_cart->getCartNum('db',array('buyer_id'=>$_SESSION['member_id']));
            Tpl::output("cart_num_con", $model_cart->cart_goods_num);
        }
    }

    public function infoOp(){
        if (!empty($_GET['uid'])){
            $member_info = nc_member_info($_GET['uid'],'uid');
        }elseif(!empty($_GET['user_name'])){
            $member_info = nc_member_info($_GET['user_name'],'user_name');
        }
        return $member_info;
    }

    public function indexOp(){
    	$member_id = $_SESSION['member_id'];
    	$model_member = Model('member');
    	$member_info = $model_member->getMemberInfo(array('member_id'=>$member_id));
        //替换昵称中表情图片 暂定unicode编码 暂时先过滤表情 by lijianjun 20170321
        //$member_info['member_name']=$member_info['member_name'].'<img src="http://www.cunyinbao.com/data/upload/emoji/smiles_01_01.png">';
    	$message_model = Model('message');
        $countnum = $message_model->countNewMessage($member_id);
        Tpl::output('msg_num_con',$countnum);
    	Tpl::output('member_info',$member_info);
        Tpl::showpage('member');
    }
    
    public function profileOp(){
    	$member_id = $_SESSION['member_id'];
    	$model_member = Model('member');
    	$member_info = $model_member->getMemberInfo(array('member_id'=>$member_id));
        //var_dump($member_info['member_avatar']);exit;
    	Tpl::output('member_info',$member_info);
    	Tpl::showpage('profile');
    }

    //ajax上传图片  by lijianjun 2017.1.7
    public function ajax_upload_imageOp(){
        $member_id=$_SESSION['member_id'];
        $max_num=addslashes($_GET['max_num']); //扩展多图上传用
        $data=array();
        //echo $member_id;exit;
        if(!empty($_FILES['image0']['tmp_name'])){
            //上传图片
            $upload = new UploadFile();
            $upload->set('max_size', 1024*3);   //设置可上传图片最大大小
            $upload->set('allow_type', array('gif', 'jpg', 'jpeg', 'png'));  //允许的上传图片类型
            $upload->set('thumb_width', 120);
            $upload->set('thumb_height',120);
            $ext = strtolower(pathinfo($_FILES['image0']['name'], PATHINFO_EXTENSION));
            $upload->set('file_name',"avatar_$member_id.$ext");
            $upload->set('thumb_ext','_new');
            $upload->set('ifremove',true);
            $upload->set('default_dir',ATTACH_AVATAR);
            $result = $upload->upfile('image0');
            //上传失败
            if (!$result){
                $data['error']=$upload->error;echo json_encode($data);exit;
            }else{
                //更新到数据库
                $model_member = Model("member");
                $model_member->editMember(array('member_id'=>$member_id),array('member_avatar'=>$upload->thumb_image));
                //返回Json
                $data['image']=BASE_SITE_URL.'data/upload/'.ATTACH_AVATAR.'/'.$upload->thumb_image;
                echo json_encode($data);exit;
            }
        }
    }
    
    public function setOp(){
    	Tpl::showpage('set');
    }

    public function setsexOp(){
    	$member_id = $_SESSION['member_id'];
    	$model_member = Model('member');
    	$member_info = $model_member->getMemberInfo(array('member_id'=>$member_id));
    	Tpl::output('member_info',$member_info);
    	Tpl::showpage('setsex');
    }
    
    public function setsexvalOp(){
    	if(empty($_POST['member_id'])){
    		output_error('资料提交不正确');
    	}
    	if($_POST['sex']=='null')$_POST['sex'] = null;
    	$model_member = Model('member');
    	$result = $model_member->editMember(array('member_id'=>$_POST['member_id']),array('member_sex'=>$_POST['sex']));
    	if($result){
    		output_data($result);
    	}
    }
    
    public function setavatarOp(){
    	$member_id = $_SESSION['member_id'];
    	$model_member = Model('member');
    	$member_info = $model_member->getMemberInfo(array('member_id'=>$member_id));
    	Tpl::output('member_info',$member_info);
    	Tpl::showpage('member_avatar');
    }
      
    public function addressOp(){
       $model_address = Model('address');
       $address_list = $model_address->getAddressList(array('member_id'=>(int)$_SESSION['member_id']),'is_default DESC,address_id DESC');
       if(empty($address_list)){
       	Tpl::showpage('address');
       }else{
       	Tpl::output('address_list',$address_list);
       	Tpl::showpage('addresslist');
       } 
    } 
    
    public function editaddressOp(){
    	$model_address = Model('address');
    	$id = (int)$_GET['id'];
    	$item = $model_address->getAddressInfo(array('address_id'=>$id));
    	Tpl::output('item',$item);
    	Tpl::showpage('editaddress');
    }
    
    public function editaddressvalOp(){
        if(empty($_POST['member_id'])){
    		output_error('资料提交不正确');
    	}
    	$model_address = Model("address");
        if(!empty($_POST['is_default'])){
    		$model_address->where(array('member_id'=>$_POST['member_id']))->update(array('is_default'=>0));
    	}
    	$address_data = array('true_name'=>$_POST['receiver'],'mob_phone'=>$_POST['mobile'],'area_info'=>$_POST['area_info'],'address'=>$_POST['address'],'is_default'=>(int)$_POST['is_default']);
        $area_list = explode(" ",$_POST['area_info']);
    	if(!empty($area_list)){
    		$model_area = Model("area");
    		$areas = $model_area->getAreaNames();
    		$area_keys = array_flip($areas);
            if(count($area_list)>1){
                $city_id = $area_keys[$area_list[1]];
                $address_data['city_id'] = $city_id;
            }
            if(count($area_list)>2){
                $area_id = $area_keys[$area_list[2]];
                $address_data['area_id'] = $area_id;
            }
    	}
    	$result = $model_address->editAddress($address_data,array('address_id'=>(int)$_POST['address_id'],'member_id'=>(int)$_POST['member_id']));
    	output_data(true);
    	
    }
    
    public function deladdressOp(){
    	$model_address = Model('address');
    	$id = (int)$_POST['id'];
    	$item = $model_address->getAddressInfo(array('address_id'=>$id));
    	if($item['member_id']!=$_SESSION['member_id']){
    		output_error('数据验证失败');
    	}else{
    	  $result = $model_address->delAddress(array('address_id'=>$id));
    	  output_data($result);
    	}
    }

    public function addaddressOp(){
       Tpl::showpage('addaddress');
    }
    
    public function addaddressvalOp(){
        if(empty($_POST['member_id'])){
    		output_error('资料提交不正确');
    	}
    	$model_address = Model("address");
    	if(!empty($_POST['is_default'])){
    		$model_address->where(array('member_id'=>$_POST['member_id']))->update(array('is_default'=>0));
    	}
    	$address_data = array('member_id'=>$_POST['member_id'],'true_name'=>$_POST['receiver'],'area_info'=>$_POST['area_info'],'address'=>$_POST['address'],'mob_phone'=>$_POST['mobile'],'is_default'=>$_POST['is_default']);
    	$area_list = explode(" ",$_POST['area_info']);
    	if(!empty($area_list)){
    		$model_area = Model("area");
    		$areas = $model_area->getAreaNames();
    		$area_keys = array_flip($areas);
            if(count($area_list)>1){
                $city_id = $area_keys[$area_list[1]];
                $address_data['city_id'] = $city_id;
            }
            if(count($area_list)>2){
                $area_id = $area_keys[$area_list[2]];
                $address_data['area_id'] = $area_id;
            }
    	}
    	$result = $model_address->addAddress($address_data);
    	if($result){
    		output_data($result);
    	}
    }
    
    public function accountsafeOp(){
    	$member_id = $_SESSION['member_id'];
    	$model_member = Model('member');
    	$member_info = $model_member->getMemberInfo(array('member_id'=>$member_id));
    	Tpl::output('member_info',$member_info);
    	Tpl::showpage('accountsafe');
    }

    public function updatepasswordOp(){
    	$member_id = $_SESSION['member_id'];
    	Tpl::output('member_id',$member_id);
    	Tpl::showpage('updatepassword');
    }
    
    public function updatepasswordvalOp(){
      if(empty($_POST['member_id'])){
    		output_error('资料提交不正确');
      }
      $model_member = Model("member");
      $item = $model_member->getMemberInfo(array('member_id'=>(int)$_POST['member_id']));
      if($item['member_passwd']!=md5(trim($_POST['old_passwd']))){
      	 output_error('旧密码不正确');
      }else{
        $result = $model_member->editMember(array('member_id'=>(int)$_POST['member_id']),array('member_passwd'=>md5(trim($_POST['password']))));
        if($result){
        	output_data($result);
        }else{
        	 output_error('密码修改失败!');
        }
      }
    }
    
    public function uploadavatarOp(){
        $member_id = $_SESSION['member_id'];
        //上传图片
        $upload = new UploadFile();
        $upload->set('thumb_width', 500);
        $upload->set('thumb_height',499);
        $ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
        $upload->set('file_name',"avatar_$member_id.$ext");
        $upload->set('thumb_ext','_new');
        $upload->set('ifremove',true);
        $upload->set('default_dir',ATTACH_AVATAR);
        if (!empty($_FILES['file']['tmp_name'])){
            $result = $upload->upfile('file');
            if (!$result){
                showMessage($upload->error,'','html','error');
            }else{
            	$model_member = Model("member");
            	$model_member->editMember(array('member_id'=>$member_id),array('member_avatar'=>$upload->thumb_image));
                echo '<script lanuguage="javascript">parent.location.reload();</script>';exit;
            }
        }else{
            showMessage('上传失败，请尝试更换图片格式或小图片','','html','error');
        }
        
    }
    
    public function updatenicknameOp(){
     if(empty($_POST['member_id'])){
    		output_error('资料提交不正确');
      }
      $model_member = Model("member");
      $result = $model_member->editMember(array('member_id'=>(int)$_POST['member_id']),array('member_nickname'=>$_POST['member_nickname']));
      if($result){
        	output_data($result);
        }else{
        	 output_error('昵称修改失败!');
        }
    }
    
     //实名认证
    public function realname_attestationOp(){
       $model_member = Model("member");
       $item = $model_member->getMemberInfo(array('member_id'=>(int)$_SESSION['member_id']));
       Tpl::output('member_info',$item);	
       Tpl::showpage('realname_attestation');
    } 
        
    public function edittrueidcardOp(){
    if(empty($_POST['member_id'])){
    		output_error('资料提交不正确');
      }
      $model_member = Model("member");
      $result = $model_member->editMember(array('member_id'=>(int)$_POST['member_id']),array('member_idcard'=>$_POST['idcard'],'member_truename'=>$_POST['name']));
      if($result){
        	output_data($result);
        }else{
        	 output_error('身份证号修改失败!');
        }
    }
    
    //设置支付密码
    public function paymentpasswordOp(){
       Tpl::output('member_id',$_SESSION['member_id']);
       $member_id = $_SESSION['member_id'];
       $model_member = Model('member');
       $member_info = $model_member->getMemberInfo(array('member_id'=>$member_id));
       Tpl::output('member_info',$member_info);
       Tpl::showpage('paymentpassword');
    }

    //修改支付密码
    public function resetpaymentvalOp(){
      if(empty($_POST['member_id'])){
    		output_error('资料提交不正确');
      }
      $model_member = Model("member");
      $member_id = (int)$_POST['member_id'];
      $member_info = $model_member->getMemberInfo(array('member_id'=>$member_id));
      if(!empty($_POST['old_passwd']) && md5($_POST['old_passwd'])!=$member_info['member_paypwd']){
      	output_error('旧密码不正确');
      }
      $result = $model_member->editMember(array('member_id'=>$member_id),array('member_paypwd'=>md5(trim($_POST['password']))));
      if($result){
        	output_data($result);
        }else{
        	 output_error('支付密码修改失败!');
        }
    }
    
    //绑定手机号
    public function bindphoneOp(){
      $model_member = Model("member");
      $item = $model_member->getMemberInfo(array('member_id'=>(int)$_SESSION['member_id']));
      Tpl::output('member_info',$item);	
      Tpl::showpage('bindphone');
    }
    
    public function bindnewphoneOp(){
    	$phone = $_POST['mobile'];
    	$captcha = $_POST['captcha'];
    	$member_id = $_POST['member_id'];
    	$type = 2;
         if (strlen($phone) == 11 && strlen($captcha) == 6){
            $condition = array();
            $condition['log_phone'] = $phone;
            $condition['log_captcha'] = $captcha;
            $condition['log_type'] = $type;
            $model_sms_log = Model('sms_log');
            $sms_log = $model_sms_log->getSmsInfo($condition);

           if(empty($sms_log) || ($sms_log['add_time'] < TIMESTAMP-1800)) {//半小时内进行验证为有效
                output_error('动态码错误或已过期，重新输入');
            }else{
            	$model_member = Model("member");
            	$model_member->editMember(array('member_id'=>$member_id),array('member_mobile'=>$phone,'member_mobile_bind'=>1));
            	output_data(true);
            }
           }else{
             output_error('格式不正确');
           }
    }
    
   //绑定邮箱
    public function bindmailboxOp(){
      $model_member = Model("member");
      $item = $model_member->getMemberInfo(array('member_id'=>(int)$_SESSION['member_id']));
      Tpl::output('member_info',$item);	
      Tpl::showpage('bindmailbox');
    }
    
    public function feedbackOp(){//意见反馈
        Tpl::showpage('feedback');
    }
    
    public function bindnewemailOp(){
    	$email = $_POST['email'];
    	$captcha = $_POST['captcha'];
    	$member_id = $_POST['member_id'];
    	
         if ($captcha==$_SESSION['mail_captcha']){
            	$model_member = Model("member");
            	$model_member->editMember(array('member_id'=>$member_id),array('member_email'=>$email,'member_email_bind'=>1));
            	output_data(true);
           }else{
             output_error('验证码不正确');
           }
    }

    public function coupons_listOp(){//我的优惠券
        $model = Model('voucher');
        $list = $model->getMemberVoucherList($_SESSION['member_id'], $_GET['select_detail_state']);

        // 取已经使用过并且未有voucher_order_id的优惠券的订单ID
        $used_voucher_code = array();
        $voucher_order = array();
        if (! empty($list)) {
            foreach ($list as $v) {
                if ($v['voucher_state'] == 2 && empty($v['voucher_order_id'])) {
                    $used_voucher_code[] = $v['voucher_code'];
                }
            }
        }
        if (! empty($used_voucher_code)) {
            $order_list = Model('order')->getOrderCommonList(array(
                'voucher_code' => array(
                    'in',
                    $used_voucher_code
                )
            ), 'order_id,voucher_code');
            if (! empty($order_list)) {
                foreach ($order_list as $v) {
                    $voucher_order[$v['voucher_code']] = $v['order_id'];
                    $model->editVoucher(array(
                        'voucher_order_id' => $v['order_id']
                    ), array(
                        'voucher_code' => $v['voucher_code']
                    ));
                }
            }
        }

        Tpl::output('list', $list);
        Tpl::showpage('coupons_list');
    }
}
